﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Utility.Converter;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Core.Global;
using BIS.Portal.Core.Base;

namespace BIS.Portal.Modules.CMS.Adm
{
    public partial class AdminCmsArticleIndependentUpdate : ControlBase
    {
        private int ArtcileId
        {
            get
            {
                return Functions.GetInt(Request.QueryString[QueryStringKeys.ArticleId]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (hiddenIsPostBack.Value.Equals("false"))
            {
                ValidateData();
            }
        }

        public void ValidateData()
        {
            string action = Request.QueryString[QueryStringKeys.ArticleUpdateAction];
            if (!string.IsNullOrEmpty(action) && action.Equals("0"))
            {
                // xu ly copy
                btnAddNew.Visible = true;
                btnUpdate.Visible = false;
            }
            BindDataIntoForm();
        }

        #region Bind Data Into Form
        public void BindDataIntoForm()
        {

            AdminCmsArticleIndependentEntity list = AdminCmsArticleBO.GetByArticleIndependentId(ArtcileId);
            if (list == null)
            {
                return;
            }
            txtArticleTitle.Text = list.Article_title;
            txtArticleContent.Text = list.Article_content;
            ddlLanguage.SelectedValue = list.Language_in_use_id.ToString();
            ddlStatus.SelectedValue = list.Status.ToString();

        }
        #endregion

        #region Save Data In Control

        public AdminCmsArticleIndependentEntity GetDataForm()
        {
            AdminCmsArticleIndependentEntity entity;

            if (ArtcileId == 0)
            {
                entity = new AdminCmsArticleIndependentEntity();
                entity.Post_date = DateTime.Now;
                entity.Last_date = DateTime.Now;
                entity.Last_update_by = "SYSTEM";
                entity.Is_hot_article = 0;
                entity.View_count = 0;
            }
            else
            {
                entity = AdminCmsArticleBO.GetByArticleIndependentId(ArtcileId);
            }

            if (!string.IsNullOrEmpty(txtArticleTitle.Text))
            {
                entity.Article_title = txtArticleTitle.Text;
            }
            else
            {
                txtArticleTitle.Focus();
            }

            if (!string.IsNullOrEmpty(txtArticleContent.Text))
            {
                entity.Article_content = txtArticleContent.Text;
            }
            else
            {
                txtArticleContent.Focus();
                return null;
            }
            int temps;
            if (ddlLanguage != null)
            {
                int.TryParse(ddlLanguage.SelectedValue, out temps);
                entity.Language_in_use_id = temps;
            }
            if (ddlStatus != null)
            {
                entity.Status = int.Parse(ddlStatus.SelectedValue.ToString());
            }
            return entity;
        }

        #endregion

        #region UpdateArticle
        private void UpdateArticle()
        {

            AdminCmsArticleIndependentEntity entity = GetDataForm();
            if (entity == null)
            {
                Label1.Text = "Error over here";
                return;
            }
            AdminCmsArticleBO.UpdateArticleIndependent(entity);
            string urlLink = WebHelpers.GetLink(UrlRewriteRules.AddArticle, CmsArticleIndependent);
            Response.Redirect(urlLink);
        }
        #endregion

        #region AddArticle
        public void AddArticle()
        {
            AdminCmsArticleIndependentEntity bisArticle = GetDataForm();
            if (bisArticle == null)
            {
                Label1.Text = "Error over here";
                return;
            }
            AdminCmsArticleBO.AddArticleIndependent(bisArticle);
            string urlLink = WebHelpers.GetLink(UrlRewriteRules.AddArticle, CmsArticleIndependent);
            Response.Redirect(urlLink);
        }
        #endregion

        #region GetDataSetting
        public string CmsArticleIndependent
        {
            get
            {
                return GetControlSetting<string>(
                  ModuleSettingNames.AdminCmsArticleIndependentUpdateControl.AdminCmsArticleIndependentUpdate_ContentPage, PortalContext.LanguageAlias
                    );
            }
        }
        #endregion

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddArticle();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateArticle();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string urlLink = WebHelpers.GetLink(UrlRewriteRules.AddArticle, CmsArticleIndependent);
            Response.Redirect(urlLink);
        }
    }
}